﻿using System;
using System.Collections.Generic;
using DataAccessLibrary.DAL;
using entities;

namespace BusinessLogicLibrary
{
    public class RoomModule
    {
        private readonly RoomDAO _roomDao = new RoomDAO();

        public bool AddEquipment(string name, decimal price, bool available)
        {
            if (name.Trim() != "")
            {
                var equipmentDetail = new EquipmentDetail
                                          {
                                              Available = available,
                                              EquipmentPrice = price,
                                              EquipmentName = name
                                          };
                return _roomDao.InsertEquipment(equipmentDetail);
            }
            throw new Exception("Invalid data");
        }

        public List<EquipmentDetail> GetEquipment(bool available)
        {
            return available ? _roomDao.SelectEquipment(1) : _roomDao.SelectEquipment(-1);
        }

        public EquipmentDetail GetEquipmentByID(int id)
        {
            return _roomDao.SelectEquipmentByID(id);
        }

        public List<EquipmentDetail> GetEquipmentByName(string name)
        {
            return _roomDao.SearchEquipmentByName(name);
        }

        public bool ModifyEquipment(int id, string name, decimal price, bool available)
        {
            if (name.Trim() != "")
            {
                var equipmentDetail = new EquipmentDetail
                                          {
                                              EquipmentDetailID = id,
                                              Available = available,
                                              EquipmentName = name,
                                              EquipmentPrice = price
                                          };
                return _roomDao.UpdateEquipment(equipmentDetail);
            }
            throw new Exception("Invalid data");
        }

        public RoomType GetRoomTypeByID(int id)
        {
            return _roomDao.SelectRoomTypeByID(id);
        }

        public List<RoomType> GetRoomType()
        {
            return _roomDao.SelectRoomType();
        }

        public List<EquipmentDetail> GetEquipmentByRoomTypeID(int id)
        {
            RoomType roomType = _roomDao.SelectRoomTypeByID(id);
            return _roomDao.SelectEquipment(roomType);
        }

        public bool AddRoomType(string title, string rank, decimal price, bool bath, bool view, bool balcon,
                                List<int> equipmentDetailID)
        {
            if (title.Trim() != "")
            {
                var equipmentDetails = new List<EquipmentDetail>();
                foreach (int id in equipmentDetailID)
                {
                    equipmentDetails.Add(_roomDao.SelectEquipmentByID(id));
                }
                var roomType = new RoomType
                                   {
                                       RoomTypeTitle = title,
                                       RoomRank = rank,
                                       RoomPrice = price,
                                       Balcony = balcon,
                                       Bath = bath,
                                       View = view
                                   };
                return _roomDao.InsertRoomType(roomType, equipmentDetails);
            }
            throw new Exception("Invalid data");
        }

        public bool ModifyRoomType(int id, string title, string rank, decimal price, bool bath, bool view,
                                   bool balcony, List<int> equipmentDetailId)
        {
            if (title.Trim() != "" && rank.Trim() != "")
            {
                var equipmentDetails = new List<EquipmentDetail>();
                foreach (int eq in equipmentDetailId)
                {
                    equipmentDetails.Add(GetEquipmentByID(eq));
                }
                var roomType = new RoomType
                                   {
                                       RoomTypeID = id,
                                       RoomTypeTitle = title,
                                       RoomRank = rank,
                                       RoomPrice = price,
                                       Bath = bath,
                                       View = view,
                                       Balcony = balcony
                                   };
                return _roomDao.UpdateRoomType(roomType, equipmentDetails);
            }
            throw new Exception("Invalid data");
        }

        public List<Room> GetRoom(int available)
        {
            return _roomDao.SelectRoom(available);
        }

        public bool AddRoom(int roomId, int status, int roomTypeId)
        {
            if (status > 1 || status < -1)
            {
                throw new Exception("Invalid data");
            }
            var room = new Room
                           {
                               RoomID = roomId,
                               Status = status,
                               RoomTypeID = roomTypeId
                           };
            return _roomDao.InsertRoom(room);
        }

        public bool ModifyRoom(int roomId, int status, int roomTypeId)
        {
            if (status > 1 || status < -1)
            {
                throw new Exception("Invalid data");
            }
            var room = new Room
                           {
                               RoomID = roomId,
                               Status = status,
                               RoomTypeID = roomTypeId
                           };
            return _roomDao.UpdateRoom(room);
        }

        public Room GetRoomByID(int id)
        {
            return _roomDao.SelectRoomByID(id);
        }

        public List<Room> GetRoomByRoomTypeID(int roomTypeId)
        {
            return _roomDao.SelectRoomByRoomTypeID(roomTypeId);
        }
    }
}